package com.foreknow.spring;

import com.foreknow.bean.Admin;
import com.foreknow.service.AdminService;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ClassPathXmlApplicationContext implements BeanFactory {
    private Map<String,Object> beans = new HashMap<>();

    public ClassPathXmlApplicationContext() throws JDOMException, IOException, ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        //解析xml文件
        //1.创建一个SAXBuilder的对象（解析器）
        SAXBuilder saxBuilder = new SAXBuilder();
        //2.构造文档Document对象
        Document document = saxBuilder.build(this.getClass().getClassLoader().getResourceAsStream("beans.xml"));
        //3.通过document对象获取xml文件的根节点
        Element rootElement = document.getRootElement();
        //4.获取根节点下的子节点的List集合
        List<Element> list = rootElement.getChildren();
        //5.遍历list集合获取每个子节点
        for(int i = 0;i<list.size();i++){
            Element element = list.get(i);
            //获取到当前节点的id和class属性值
            String id = element.getAttributeValue("id");
            String clazz = element.getAttributeValue("class");
            Object o = Class.forName(clazz).newInstance(); //创建对象AdminServiceImpl
            //将id和clazz添加到集合中
            beans.put(id,o);
            //获取到bean中的property
            for(Element propertyElement:element.getChildren()) {
                String name = propertyElement.getAttributeValue("name"); //adminDao
                String bean = propertyElement.getAttributeValue("bean"); //u-----AdminDaoImpl
                Object beanObject = beans.get(bean); //AdminDaoImpl
                //通过反射的机制动态调用 public void setAdminDao(AdminDao adminDao)
                String methodName = "set" + name.substring(0,1).toUpperCase() + name.substring(1); //setAdminDao
                //获取方法
                Method method = o.getClass().getMethod(methodName,beanObject.getClass().getInterfaces()[0]);
                //调用方法 o表示setAdminDao所属的对象  beanObject 实际参数 AdminDaoImpl
                method.invoke(o,beanObject);
            }
        }
    }

    @Override
    public Object getBean(String id) {
        return beans.get(id);
    }

    public static void main(String[] args) {
        try {
            BeanFactory beanFactory = new ClassPathXmlApplicationContext();
            AdminService service = (AdminService) beanFactory.getBean("adminService"); //AdminServiceImpl
            service.add(new Admin());
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
